package base.Controller;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.stage.Stage;
import java.io.File;
import java.util.List;

public class SlideshowController {
    @FXML private ImageView imageView;
    @FXML private Button prevButton;
    @FXML private Button playButton;
    @FXML private Button nextButton;
    @FXML private Button zoomInButton;
    @FXML private Button zoomOutButton;
    @FXML private Button exitButton;

    private List<File> imageFiles;
    private int currentIndex = 0;
    private Timeline slideshow;
    private double zoomFactor = 1.0;

    @FXML
    public void initialize() {
        // 设置按钮样式
        prevButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");
        playButton.setStyle("-fx-background-color: rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");
        nextButton.setStyle("-fx-background-color: rgb(91, 92, 91);-fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");
        zoomInButton.setStyle("-fx-background-color: rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");
        zoomOutButton.setStyle("-fx-background-color: rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");
        exitButton.setStyle("-fx-background-color: rgb(91, 92, 91);-fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;");

        // 鼠标悬停效果
        prevButton.setOnMouseEntered(e -> prevButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        prevButton.setOnMouseExited(e -> prevButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        playButton.setOnMouseEntered(e -> playButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        playButton.setOnMouseExited(e -> playButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        nextButton.setOnMouseEntered(e -> nextButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        nextButton.setOnMouseExited(e -> nextButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        zoomInButton.setOnMouseEntered(e -> zoomInButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        zoomInButton.setOnMouseExited(e -> zoomInButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        zoomOutButton.setOnMouseEntered(e -> zoomOutButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        zoomOutButton.setOnMouseExited(e -> zoomOutButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        exitButton.setOnMouseEntered(e -> exitButton.setStyle("-fx-background-color:#A4A6A4; -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        exitButton.setOnMouseExited(e -> exitButton.setStyle("-fx-background-color:rgb(91, 92, 91); -fx-text-fill: white; -fx-font-size: 14px; -fx-padding: 5px 15px;"));
        // 其他按钮类似设置鼠标悬停效果...

        // 设置按钮事件
        prevButton.setOnAction(e -> showPreviousImage());
        nextButton.setOnAction(e -> showNextImage());
        playButton.setOnAction(e -> toggleSlideshow());
        zoomInButton.setOnAction(e -> zoom(1.2));
        zoomOutButton.setOnAction(e -> zoom(0.8));
        exitButton.setOnAction(e -> closeWindow());

        // 初始化幻灯片播放Timeline
        slideshow = new Timeline(new KeyFrame(Duration.seconds(2), e -> showNextImage()));
        slideshow.setCycleCount(Timeline.INDEFINITE);
    }

    public void setImageFiles(List<File> files, int startIndex) {
        this.imageFiles = files;
        this.currentIndex = startIndex;
        showCurrentImage();
    }

    private void showCurrentImage() {
        if (currentIndex >= 0 && currentIndex < imageFiles.size()) {
            File file = imageFiles.get(currentIndex);
            Image image = new Image(file.toURI().toString());
            imageView.setImage(image);

            // 调整图片大小以适应窗口
            imageView.setFitWidth(800);
            imageView.setFitHeight(600);
        }
    }

    private void showPreviousImage() {
        if (currentIndex > 0) {
            currentIndex--;
            showCurrentImage();
        }
        else{
            showMessage("已是第一张图片哦~");
        }
    }

    private void showNextImage() {
        if (currentIndex < imageFiles.size() - 1) {
            currentIndex++;
            showCurrentImage();
        } else {
            // 到达最后一张图片时停止播放
            showMessage("已是最后一张图片哦~");

            Image image = new Image(getClass().getResource("/icons/play.png").toExternalForm());
            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(32);
            imageView.setFitHeight(32);
            playButton.setGraphic(imageView);
            slideshow.stop();

            //playButton.setText("播放");
        }
    }

    private void toggleSlideshow() {
        Image image;
        if (slideshow.getStatus() == Timeline.Status.RUNNING) {
            slideshow.stop();
            image = new Image(getClass().getResource("/icons/play.png").toExternalForm());
            //playButton.setText("播放");
        } else {
            slideshow.play();
            image = new Image(getClass().getResource("/icons/stop.png").toExternalForm());
            //playButton.setText("暂停");
        }
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(32);
        imageView.setFitHeight(32);
        playButton.setGraphic(imageView);
    }

    private void zoom(double factor) {
        zoomFactor *= factor;
        imageView.setScaleX(zoomFactor);
        imageView.setScaleY(zoomFactor);
    }

    private void closeWindow() {
        slideshow.stop();
        ((Stage) exitButton.getScene().getWindow()).close();
    }

    private void showMessage(String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.show();
    }


}
